package com.beyond.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.beyond.core.service.CommuniationService;

import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;

/**
 * 主通讯服务器程序WEB入口
 *
 * @author wyd
 * @date 2020年9月21日-下午2:34:54
 */
@SpringBootApplication
@Slf4j
public class ServerApp implements CommandLineRunner {

    @Autowired
    CommuniationService communiationService;

    public static void main(String[] args) {
        SpringApplication.run(ServerApp.class);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("准备开启通讯服务线程。。。");

        ChannelFuture f = communiationService.startService();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                communiationService.destroyService();
            }
        });
        //服务端管道关闭的监听器并同步阻塞,直到channel关闭,线程才会往下执行,结束进程
        f.channel().closeFuture().syncUninterruptibly();
    }
}
